package com.kefan.reptilemanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.datatype.jsr310.ser.YearSerializer;
import com.kefan.reptilemanagement.common.entities.Application;
import com.kefan.reptilemanagement.common.entities.Spider;
import com.kefan.reptilemanagement.common.entities.SysUser;
import com.kefan.reptilemanagement.common.errorHandler.BizException;
import com.kefan.reptilemanagement.mapper.SpiderMapper;
import com.kefan.reptilemanagement.service.ApplicationService;
import com.kefan.reptilemanagement.service.SpiderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kefan.reptilemanagement.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sjj
 * @since 2024-05-03
 */
@Service
public class SpiderServiceImpl extends ServiceImpl<SpiderMapper, Spider> implements SpiderService {

    @Autowired
    ApplicationService applicationService;
    @Autowired
    SysUserService sysUserService;
    @Override
    public Spider addAndCompeleteApplication(Spider spider) {
        spider.setCreated(LocalDateTime.now());
        spider.setUpdated(LocalDateTime.now());
        //获取当前用户名
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //获取当前用户
        SysUser sysUser = sysUserService.getByUsername(username);
        Application application=applicationService.getById(spider.getApplicationId());
        if(application!=null)
        {
            application.setStatus(3);
            applicationService.updateById(application);
        }
        spider.setTechId(sysUser.getId());

        save(spider);
        return spider;
    }

    @Override
    public List<Spider> getList(String name, Integer status, Long tech_id) {
        QueryWrapper<Spider>wrapper=new QueryWrapper<>();
        if(name!=null&&!name.isEmpty())
        {
            wrapper.eq("name",name);
        }
        if(status!=null)
        {
            wrapper.eq("status",status);
        }
        if(tech_id!=null)
        {
            wrapper.eq("tech_id",tech_id);
        }
        return list(wrapper);
    }

    @Override
    public List<Spider> getCurrentSpiderList() {
        //获取当前用户名
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //获取当前用户
        SysUser sysUser = sysUserService.getByUsername(username);

        QueryWrapper<Spider>wrapper=new QueryWrapper<>();

        wrapper.eq("tech_id",sysUser.getId());

        return list(wrapper);

    }

    @Override
    public Spider delete(Long id) {
        Spider spider=getById(id);
        if(spider==null)
        {
            throw  new BizException("爬虫不存在！");
        }
        removeById(id);
        return spider;
    }

    @Override
    public Spider change(Spider spider) {
        Spider old=getById(spider.getId());
        if(old==null)
        {
            throw new BizException("爬虫不存在！");
        }
        updateById(spider);
        return spider;
    }

    @Override
    public Spider upload(Long id, String content) {
        Spider spider=getById(id);
        if(spider==null)
        {
            throw new BizException("不存在该爬虫！");
        }
        spider.setFile(content);
        updateById(spider);
        return spider;
    }
}
